package com.raojunxue.company.interceptor;

import org.apache.struts2.ServletActionContext;

import com.google.appengine.repackaged.org.apache.commons.logging.Log;
import com.google.appengine.repackaged.org.apache.commons.logging.LogFactory;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

/**
 * 
 *
 */
public class PerformanceAnalysisInterceptor implements Interceptor {

	private static final long serialVersionUID = 1L;

	private final Log log = LogFactory.getLog(PerformanceAnalysisInterceptor.class);
	
	public void destroy() {
	}

	public void init() {
	}


	public String intercept(ActionInvocation ai) throws Exception {
		long start = System.currentTimeMillis();
		long threadID = Thread.currentThread().getId();
		
		String requestPath = ServletActionContext.getRequest().getServletPath();
		log.info("\t[Thread "+threadID +"] receive request \"" +requestPath+ "\"");
		
		String result = ai.invoke();
		
		long duration = System.currentTimeMillis() - start;
		log.info("\t[Thread "+threadID +"] complete request \"" +requestPath+ "\" with total time " + duration + " ms\n");
		return result;
	}

}
